var detafeed_historyTime = 0
var detafeed_lastResolution = null
var detafeed_lastSymbol = null
var t_init = null;
var urls = "http://47.244.154.91:9090/quotations/getKlineDataByIntervalTimeForHttp"
var datafeed = {
	getApiTime: (resolution) => {
		switch (resolution) {
			case '1':
				return '1min'
			case '3':
				return '3min'
			case '5':
				return '5min'
			case '15':
				return '15min'
			case '30':
				return '30min'
			case '60':
				return '60min'
			case '240':
				return '4h'
			case 'D':
				return '1day'
			case 'W':
				return '1week'
			default:
				return '15m'
		}
	},
	getSendSymbolName: (symbolName, index) => {
		var name = symbolName.split('/');
		return (name[index]).toLocaleUpperCase()
	},
	onReady: (callback) => {
		console.log("=====onReady running");
		callback()
	},
	resolveSymbol: (symbolName, onSymbolResolvedCallback, onResolveErrorCallback) => {
		var symbol_stub = {
			name: symbolName,
			description: "",
			has_intraday: true,
			has_no_volume: false,
			minmov: 1,
			minmov2: 2,
			pricescale: 100,
			session: "24x7",
			supported_resolutions: ["1", "5", "15", "30", "60", "1D", "1W"],
			ticker: symbolName,
			timezone: "Asia/Shanghai",
			type: "stock"
		}
		setTimeout(() => onSymbolResolvedCallback(symbol_stub), 0);
	},
	getBars: function(symbolInfo, resolution, from, to, onHistoryCallback, onErrorCallback, firstDataRequest) {
		if (!detafeed_historyTime || (resolution !== detafeed_lastResolution) || detafeed_lastSymbol !== symbolInfo.name) { // 储存请求过的产品 
			detafeed_lastSymbol = symbolInfo.name // 记录目前时间搓，就用目前的目前时间搓往前请求历史数据    
			detafeed_historyTime = Date.now()
		}
		clearInterval(t_init)
		const e_time = Number((Date.now() + '').substr(0, 9) + '0000');
		// 参数
		const r_data = {
			// intervalTime: this.getApiTime(resolution).replace('min', ''),
			intervalTime: this.getApiTime(resolution),
			childCoinName: this.getSendSymbolName(symbolInfo.name, 0),
			motherCoinName: this.getSendSymbolName(symbolInfo.name, 1)
		}
		$.ajax({
			type: 'GET',
			url: urls,
			contentType: 'application/x-www-form-urlencoded',
			data: r_data,
			dataType: 'json',
			success: function(data) {
				if (data.data && Array.isArray(data.data)) { // 记录这次请求的时间周期    
					const k_list = data.data;
					detafeed_lastResolution = resolution;
					var meta = {
						noData: false
					};
					var bars = []
					if (k_list.length) {
						localStorage.setItem('k_open', Number(k_list[k_list.length - 1].open).toFixed(2))
						localStorage.setItem('k_high', Number(k_list[k_list.length - 1].high).toFixed(2))
						localStorage.setItem('k_low', Number(k_list[k_list.length - 1].low).toFixed(2))
						localStorage.setItem('k_close', Number(k_list[k_list.length - 1].close).toFixed(2))
						localStorage.setItem('k_volume', Number(k_list[k_list.length - 1].volume).toFixed(2))
						localStorage.setItem('k_time', k_list[k_list.length - 1].createTime);
						detafeed_historyTime = e_time;
						for (var i = 0; i < k_list.length; i++) {
							bars.push({
								time: k_list[i].timeStamp * 1,
								close: k_list[i].close,
								open: k_list[i].open,
								high: k_list[i].high,
								low: k_list[i].low,
								volume: k_list[i].volume
							})
						}
						meta = {
							noData: true
						}
					} else {
						meta = {
							noData: true
						}
					}
					onHistoryCallback(bars, meta);
				}
			},
			onErrorCallback: function(message) {
				console.log(message)
			}
		})
	},
	subscribeBars: function(symbolInfo, resolution, onRealtimeCallback, subscribeUID, onResetCacheNeededCallback) {
		t_init = setInterval(() => {
			const e_time = Number((Date.now() + '').substr(0, 9) + '0000')
			const r_data = {
				intervalTime: this.getApiTime(resolution).replace('min', ''),
				childCoinName: this.getSendSymbolName(symbolInfo.name, 0),
				motherCoinName: this.getSendSymbolName(symbolInfo.name, 1)
			}
			$.ajax({
				type: 'GET',
				url: urls,
				contentType: 'application/x-www-form-urlencoded',
				data: r_data,
				dataType: 'json',
				success: function(data) {
					if (data.data && Array.isArray(data.data)) { // 记录这次请求的时间周期    
						const k_list = data.data;
						k_list.reverse();
						if (k_list.length) {
							detafeed_historyTime = e_time;
							if (localStorage.k_time == k_list[0].createTime) {
								for (var i = 0; i < 1; i++) {
									onRealtimeCallback({
										time: Number(k_list[i].timeStamp),
										close: k_list[i].close,
										open: k_list[i].open,
										high: k_list[i].high,
										low: k_list[i].low,
										volume: k_list[i].volume
									})
								}
							} else {
								for (var i = 0; i < k_list.length; i++) {
									onRealtimeCallback({
										time: Number(k_list[i].timeStamp),
										close: k_list[i].close,
										open: k_list[i].open,
										high: k_list[i].high,
										low: k_list[i].low,
										volume: k_list[i].volume
									})
								}
							}
							localStorage.setItem('k_time', k_list[0].createTime);
						}
					}
				},
				onErrorCallback: function(message) {
					console.log(message)
				}
			})
		}, 10000)
	},

}
